<?php

namespace App\Http\Controllers\Index;

use App\Models\User;
use App\Http\Controllers\Controller;
use App\Models\WechatUser;
use Illuminate\Support\Facades\Config;
use Tymon\JWTAuth\Facades\JWTAuth;

class LoginController extends Controller
{
	/**
	 * 微信登录
	 * User: Clannad ~ ☆
	 */
	public function login()
    {
		return view('index.login.login');
    }


    /**
     * 微信登录页面
     * User: Clannad ~ ☆
     */
    public function wxLogin()
    {
        $user = session('wechat.oauth_user.default');

//        echo '<pre>';
//        var_dump($user);
        $status = 0; //返回给前端判断是否授权成功
//        app()->instance(\Illuminate\Contracts\Auth\Guard::class, auth('api'));
        //获取到了用户的基本信息
        if($user){
            //根据open_id去查用户
            $app_user = User::query()->where([
                            'wx_openid'=>$user->id
                        ])->first();

            //状态不对的用户直接不能登录
            if($app_user->status == 0){
                $url = 'http://localhost:8080/#/wx_login_auth';
//        $url = 'http://clannad.17hxg.com/#/wx_login_auth?token=' . $token;
                return redirect($url);
//                header('Location:'.$url);
//                die
            }


            //如果有 就颁发token
            if($app_user){
                $checked = WechatUser::query()->where(['user_id'=>$app_user->id])->first();

                if($checked){
                    //存储access_token
                    WechatUser::query()->where([
                        'user_id'=>$app_user->id,
                    ])->update(['access_token'=>$user->token]);
                }else{
                    //存储access_token
                    WechatUser::query()->where([
                        'user_id'=>$app_user->id,
                    ])->create(['access_token'=>$user->token]);
                }
//                Auth::generateTokenById(1);
//                $token = \Auth::generateTokenById($app_user->id);
                $token = \Auth::guard('api')->fromSubject($app_user);
//                $token = JWTAuth::fromUser($app_user);
            }else{
                //自动创建用户
                $userData = [
                    'username'    => 'cl_' . time() . mt_rand(1111, 9999),
                    'nick_name'   => $user->nickname,
                    'password'    => bcrypt(123456),
                    'wx_username' => $user->nickname,
                    'thumb'       => $user->avatar,
                    'wx_openid'   => $user->id,
                ];

                $app_user = User::create($userData);
                $app_user->user_info()->create([
                   'age'=>1,
                   'sex'=>0
                ]);

                //存储access_token
                WechatUser::query()->create([
                    'user_id'=>$app_user->id,
                    'access_token'=>$user->token
                ]);

                auth('api')->onceUsingId($app_user->id);
                $token = JWTAuth::fromUser($app_user);
            }
            $status = 1;
//            throw new ResponseException(200,'ok',['token'=>$token]);
        }else{
            $token = '';
        }

        $url = 'http://localhost:8080/#/wx_login_auth?token=' . $token;
//        $url = 'http://clannad.17hxg.com/#/wx_login_auth?token=' . $token;
        header('Location:'.$url);

//        return view('index.login.wxlogin',compact('status','token'));
    }

    	//使用了中间件，可以直接获取
		/*$user = session('wechat.oauth_user.default');

		//获取到了用户的基本信息
		if($user){
			//根据信息去查用户
			throw new ResponseException(200,'ok',['code'=>$user->id]);
		}else{

		}*/

		//不使用中间件的办法

}
